package cn.edu.jsu.zjj.controller;


import cn.edu.jsu.zjj.annotation.AuthCheck;
import cn.edu.jsu.zjj.annotation.Type;
import cn.edu.jsu.zjj.common.DeleteRequest;
import cn.edu.jsu.zjj.common.result.PageResult;
import cn.edu.jsu.zjj.common.result.Result;
import cn.edu.jsu.zjj.constant.MessageConstant;
import cn.edu.jsu.zjj.constant.UserConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.exception.ThrowUtils;
import cn.edu.jsu.zjj.model.dto.usercourse.UserCourseAddMyRequest;
import cn.edu.jsu.zjj.model.dto.usercourse.UserCourseQueryRequest;
import cn.edu.jsu.zjj.model.dto.usercourse.UserCourseUpdateMyRequest;
import cn.edu.jsu.zjj.model.entity.User;
import cn.edu.jsu.zjj.model.entity.UserCourse;
import cn.edu.jsu.zjj.service.UserCourseService;
import cn.edu.jsu.zjj.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 所授课程相关接口
 */
@RestController
@RequestMapping("/userCourse")
@Slf4j
@Api(tags = "所授课程管理接口")
public class UserCourseController {
    @Autowired
    private UserCourseService userCourseService;

    @Autowired
    private UserService userService;

    @ApiOperation("所授课程分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody UserCourseQueryRequest userCourseQueryRequest) {
        log.info("所授课程分页查询，参数为：{}", userCourseQueryRequest);
        long current = userCourseQueryRequest.getCurrent();
        long size = userCourseQueryRequest.getPageSize();
        Page<UserCourse> userCoursePage = userCourseService.page(new Page<>(current, size),
                userCourseService.getQueryWrapper(userCourseQueryRequest));
        return Result.success(new PageResult(userCoursePage.getTotal(), userCoursePage.getRecords()));
    }

    @ApiOperation("所授课程全部查询")
    @PostMapping("/list")
    public Result list() {
        LambdaQueryWrapper<UserCourse> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserCourse::getCreateUserId, userService.getLoginUser().getCreateUserId());
        List<UserCourse> userCourseList = userCourseService.list(queryWrapper);
        return Result.success(userCourseList);
    }


    /**
     * 新增所授课程
     *
     * @param userCourseAddMyRequest
     * @return
     */
    @Type("新增所授课程")
    @ApiOperation("新增所授课程")
    @PostMapping
    public Result save(@RequestBody UserCourseAddMyRequest userCourseAddMyRequest) {
        log.info("新增所授课程：{}", userCourseAddMyRequest);
        UserCourse userCourse = new UserCourse();
        BeanUtils.copyProperties(userCourseAddMyRequest, userCourse);
        User loginUser = userService.getLoginUser();
        userCourse.setCreateUserId(loginUser.getId());
        userCourse.setCreateUserName(loginUser.getName());
        boolean result = userCourseService.save(userCourse);
        ThrowUtils.throwIf(!result, MessageConstant.OPERATION_ERROR);
        return Result.success();
    }

    /**
     * 编辑所授课程信息
     * @param userCourseUpdateMyRequest
     * @return
     */
    @Type("编辑所授课程信息")
    @ApiOperation("编辑所授课程信息")
    @PutMapping
    public Result<String> update(@RequestBody UserCourseUpdateMyRequest userCourseUpdateMyRequest) {
        log.info("编辑所授课程，请求体为：{}", userCourseUpdateMyRequest);
        if (userCourseUpdateMyRequest == null) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        UserCourse userCourse = new UserCourse();
        BeanUtils.copyProperties(userCourseUpdateMyRequest, userCourse);
        boolean result = userCourseService.updateById(userCourse);
        ThrowUtils.throwIf(!result, MessageConstant.OPERATION_ERROR);
        return Result.success("更新成功");
    }


    @Type("删除所授课程")
    @PostMapping("/delete")
    @ApiOperation("删除所授课程（仅管理员）")
    @AuthCheck(mustRoles = {UserConstant.ADMIN_ROLE, UserConstant.EDU_ADMIN_ROLE, UserConstant.MAJOR_ADMIN_ROLE})
    public Result<Boolean> deleteUserCourse(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        return Result.success(userCourseService.removeById(deleteRequest.getId()));
    }

    /**
     * 根据id查询所授课程,并且去掉isDelete=1的记录
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询所授课程")
    @GetMapping("/{id}")
    public Result<UserCourseUpdateMyRequest> getById(@PathVariable Long id) {
        if (id <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }

        UserCourse userCourse = userCourseService.getById(id);
        UserCourseUpdateMyRequest userCourseUpdateMyRequest = new UserCourseUpdateMyRequest();
        BeanUtils.copyProperties(userCourse, userCourseUpdateMyRequest);
        log.info("查询所授课程，结果为：{}", userCourseUpdateMyRequest);
        ThrowUtils.throwIf(userCourse == null, MessageConstant.NOT_FOUND_ERROR);
        return Result.success(userCourseUpdateMyRequest);
    }
}
